package com.example.hxpc.guidancemanagement.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.PhoneJudgeUtils;
import com.example.hxpc.guidancemanagement.tools.ProcessingSMSTools;
import com.example.hxpc.guidancemanagement.tools.TimeCount;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import static com.example.hxpc.guidancemanagement.R.id.back;

/**
 * Created by tao on 2017/12/5.
 */

public class ForgetPasswordActivity extends Activity implements View.OnClickListener,BaseApi{
    private TextView te_foeget_pass , te_sure , te_sms;
    private ImageView img_back , img_add;
    private RelativeLayout re_get_sms;
    private EditText edt_phone , edt_sms;
    private OkHttpTools okHttpTools = new OkHttpTools();
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    ToastTools.toastMsg(ForgetPasswordActivity.this,"验证成功");
                    Intent intent = new Intent(ForgetPasswordActivity.this,NewPasswordActivity.class);
                    intent.putExtra("telephone",edt_phone.getText().toString());
                    startActivity(intent);
                    finish();
                    break;
                case 2:
                    TimeCount timeCountUtil = new TimeCount(ForgetPasswordActivity.this, 60000, 1000, te_sms);
                    timeCountUtil.start();
                    ToastTools.toastMsg(ForgetPasswordActivity.this,"获取成功，注意查看");
                    break;
                case 3:
                    ToastTools.toastMsg(ForgetPasswordActivity.this,"获取验证码失败,请填写正确的手机号码");
                    break;
                case 4:
                    ToastTools.toastMsg(ForgetPasswordActivity.this,"验证码错误");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_forget_password);
        initView();
    }

    //初始化控件
    public void initView(){
        img_add = (ImageView) findViewById(R.id.add_mark);
        img_add.setVisibility(View.GONE);
        img_back = (ImageView) findViewById(R.id.back);
        img_back.setOnClickListener(this);
        te_foeget_pass = (TextView) findViewById(R.id.te_title);
        te_foeget_pass.setText("忘记密码");
        te_sure = (TextView) findViewById(R.id.sure);
        te_sure.setVisibility(View.VISIBLE);
        te_sure.setOnClickListener(this);
        re_get_sms = (RelativeLayout) findViewById(R.id.rela_sms);
        re_get_sms.setOnClickListener(this);
        te_sms = (TextView) findViewById(R.id.te_sms);
        edt_phone = (EditText) findViewById(R.id.forget_phone);
        edt_sms = (EditText) findViewById(R.id.forget_sms);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.sure:
                if (edt_phone.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请输入手机号");
                }else if (edt_sms.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请输入验证码");
                }else {
                    SMSSDK.submitVerificationCode("86",edt_phone.getText().toString(),edt_sms.getText().toString());
                    ProcessingSMSTools.smsFlage = 2;
                }
                break;
            case R.id.rela_sms:
                if (edt_phone.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请输入手机号");
                }else if (!PhoneJudgeUtils.judgePhoneNums(edt_phone.getText().toString())){
                    ToastTools.toastMsg(this,"手机号格式错误");
                }else {
                    okHttpTools.isExitTelephone(this,edt_phone.getText().toString());
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ProcessingSMSTools.Cancel();
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {
        if (success.equals("false")){
            ToastTools.toastMsg(this,jsonBase.getMsg());
        }else{
            ProcessingSMSTools.initReceiverCode(handler);
            //获取验证码
            SMSSDK.getVerificationCode("86",edt_phone.getText().toString());
            ProcessingSMSTools.smsFlage = 1;
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }
}
